import os
import urllib.request
import json
from tqdm import tqdm

with open('image_info_test-dev2017.json') as jsonFile:
  json_dict = json.load(jsonFile)

file_dir = 'test-dev2017'

if not os.path.exists(file_dir):
  os.makedirs(file_dir)

images_desc = tqdm(json_dict['images'])

for image in images_desc:

  url = image['coco_url']
  file_suffix = os.path.splitext(url)[1]
  filepath = '{}{}{}{}'.format(file_dir,os.sep,os.path.basename(os.path.splitext(url)[0]), file_suffix)
  
  if os.path.isfile(filepath):
    continue

  images_desc.set_description(
    'Downloading... {fileName}'.format(
    fileName=filepath)
  )
  
  try:
    urllib.request.urlretrieve(url,filename=filepath)
  except Exception as e:
    print("{} is not download! {}".format(filepath, e))
  finally:
    pass
   
images_desc.close()